# 导包
import pymysql

conn = None
my_cursor = None

try:
    # 创建连接
    conn = pymysql.connect(host="localhost", port=3306, user="root",
                       password="10086", database="student", charset="utf8")

    # 创建游标
    my_cursor = conn.cursor()

    # 执行 insert sql
    my_cursor.execute("insert course(studentId, courseId, name,type,score,nature) values(2022212813,6,'英语','必修',5,'人文类');")

    print("Affected rows:", conn.affected_rows())

    # 提交事务
    conn.commit()

except Exception as err:
    print("插入数据异常：", str(err))
    # 回滚事务
    conn.rollback()

finally:
    # 关闭游标
    my_cursor.close()

    # 关闭连接
    conn.close()